package com.sample.action;

import java.util.List;

import org.jbpm.JbpmConfiguration;
import org.jbpm.JbpmContext;
import org.jbpm.graph.def.ProcessDefinition;
import org.jbpm.graph.exe.ProcessInstance;
import org.jbpm.graph.exe.Token;
import org.junit.Test;

public class HelloworldWithDBTest {

	public static void main(String[] args) {
		JbpmContext jcontext = JbpmConfiguration.getInstance().createJbpmContext();
		
		try {
			ProcessDefinition pd = jcontext.getGraphSession().findLatestProcessDefinition("helloword");
			ProcessInstance pi = new ProcessInstance(pd);
			Token token = pi.getRootToken();
			
			System.out.println("1	当前流程所处的节点：" + token.getNode().getName());
			
			token.signal();
			System.out.println("2	当前流程所处的节点：" + token.getNode().getName());
			
			
			token.signal();
			System.out.println("3	当前流程所处的节点：" + token.getNode().getName());
			
			token.signal();
			System.out.println("4	当前流程所处的节点：" + token.getNode().getName());
			
			
			System.out.println("5	当前流程的状态：" + token.getProcessInstance().hasEnded());
			
		} finally {
			if (jcontext != null) {
				jcontext.close();
			}
		}
	}
	
	
	@Test
	public void testGetLatestProcessDefinition() {
		JbpmContext jcontext = JbpmConfiguration.getInstance().createJbpmContext();
		try {
			ProcessDefinition pd = jcontext.getGraphSession().findProcessDefinition("helloword", 1);
			System.out.println(pd.getNodes());
		} finally {
			jcontext.close();
		}
	}
	
	
	@Test
	public void testGindLatestProcessDefinitions() {
		JbpmContext jcontext = JbpmConfiguration.getInstance().createJbpmContext();
		try {
			List<?> pds = jcontext.getGraphSession().findLatestProcessDefinitions();
			System.out.println(pds);
		} finally {
			jcontext.close();
		}
	}
	
	@Test
	public void testActionHandler() {
		try {
			ProcessDefinition pd = ProcessDefinition.parseXmlResource("helloworld/processdefinition.xml");
			ProcessInstance pi = new ProcessInstance(pd);
			Token token = pi.getRootToken();
			
			System.out.println("1	当前流程所处的节点：" + token.getNode().getName());
			
			token.signal();
			System.out.println("2	当前流程所处的节点：" + token.getNode().getName());
			
			
			token.signal();
			System.out.println("3	当前流程所处的节点：" + token.getNode().getName());
			
			token.signal();
			System.out.println("4	当前流程所处的节点：" + token.getNode().getName());
			
			
			System.out.println("5	当前流程的状态：" + token.getProcessInstance().hasEnded());
			
		} finally {
		}
	}
}
